package dat255.calendarclock.view;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
import dat255.calendarclock.R;
import dat255.calendarclock.model.event.Event;
import dat255.calendarclock.model.event.EventHandler;

/**
 * 
 * 
 * @author Marcus Hultman, Oskar Montin
 */
public class TopBar extends LinearLayout implements Observer{
	
	private static TopBar instance;
	private TextView textLabel;
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("d MMMM yy");
	
	public TopBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		EventHandler.getInstance().addObserver(this);
		textLabel = (TextView) findViewById(R.id.topBar_Label);
		instance = this;
	}
	
	public static TopBar getInstance(){
		return TopBar.instance;
	}

	public void setTextLabel(TextView textLabel){
		this.textLabel = textLabel;
	}
	
	/**
	 * Sets the text on the top bar
	 * @param date date to show
	 */
	private void setText(Date date) {
		if (textLabel == null)
			throw new IllegalArgumentException("textLabel has not been set.");
		textLabel.setText(dateFormat.format(date));
	}

	@Override
	public void update(Observable observable, Object data) {
		if(data instanceof Event) {
			return;
		}
		setText(EventHandler.getInstance().getSelectedDay().getTime());	
	}
	
}
